# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: client_view.py
Author: kij
Create Date: 2021/3/18
-------------------------------------------------
"""

import socket

from conf import setting
from modle.client_modle import Clent
from interfaces import client_interface


def logon(msg, conn):
	"""
	登录方法，登录成功后拿到根目录路径并显示，还有用户信息
	:return:
	"""
	if isinstance(conn, Clent):
		user = client_interface.logon_impl(msg, conn.request)
	else:
		user = client_interface.logon_impl(msg, conn)
	return user

def register(msg, conn):
	"""
	登录方法，登录成功后拿到根目录路径并显示，还有用户信息
	:return:
	"""
	user = client_interface.register_impl(msg, conn)
	return user


def switch_fun(input_msg, conn):
	"""
	分析输入命令
	:return:
	"""
	cmd_msg = input_msg.split(' ')

	if cmd_msg[0] == 'logon':
		return logon(cmd_msg[1:], conn)
	elif cmd_msg[0] == 'register':
		return register(cmd_msg[1:], conn)
	else:
		return client_interface.func_switch(conn, str(input_msg).split(" ", maxsplit=1))
	return '输入框提示语'


def run():
	"""
	链接服务器
	:return:
	"""
	conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	conn.connect(setting.IP_POST)
	hint = '请输入>>>  '
	user = None  # 用于调用具体逻辑类的user
	while True:
		try:
			input_msg = input(hint)
			if input_msg.upper() == 'Q':
				break
			# 获取数据然后传入  链接对象类 app_client
			res = switch_fun(input_msg, user if user else conn)
			# 拿到数据逻辑处理类
			if isinstance(res, Clent):
				user = res
				hint = "%s%s>>>" %(res.name,res.context_path)
			else:
				print(res)
				hint = '%s>>>' % res

		except KeyboardInterrupt as e:
			print(".", e)